package com.stlm2.util;

import org.apache.commons.fileupload.FileItem;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * Author：fuchunyu
 * Date：2017/8/1
 * Description：设置图片水印
 */
public class ImageMarkLogoByIcon {

    public static BufferedImage markImageByIcon(String iconPath, FileItem item) {
      return   markImageByIcon(iconPath, item, null);
    }

    /**
     * 给图片添加水印、可设置水印图片旋转角度
     * @param iconPath 水印图片路径
     * @param item 要加水印的路径
     * @param degree 水印图片旋转角度
     */
    public static BufferedImage markImageByIcon(String iconPath, FileItem item, Integer degree) {
        //OutputStream os = null;
        BufferedImage buffImg=null;
        BufferedImage image=null;
        try {
             buffImg =ImageIO.read(item.getInputStream());

            //按比例调整图片大小，默认宽度为1024
            int imageHight=buffImg.getHeight();
            if(buffImg.getHeight()>1500){
                imageHight=1500;
            }
            if(buffImg.getHeight()<512){
                imageHight=1024;
            }
            if (buffImg.getWidth()>buffImg.getHeight()){
                imageHight=960;
            }

            //创建图片模板
            image = new BufferedImage(1024, imageHight, BufferedImage.TYPE_INT_BGR);

            //得到模板宽高
            int imageHeight=image.getHeight();
            int imageWidth=image.getWidth();


            //得到画笔对象
            Graphics2D g = image.createGraphics();
            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //把源图片画到模板上
            g.drawImage(buffImg.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), 0, 0, null);
            // 设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            }

            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            URL url=new URL(iconPath);
            ImageIcon imgIcon = new ImageIcon(url);

            // 得到Image对象。
            Image img = imgIcon.getImage();
            int icoHeight=img.getHeight(null)+30;
            int icoWidth=img.getWidth(null)+30;



            float alpha = 1f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            // 表示水印图片的位置
            g.drawImage(img, imageWidth-icoWidth-40, imageHeight-icoHeight-40,icoWidth,icoHeight, null);

            //g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

            g.dispose();

           //os = new FileOutputStream("C:\\Users\\apple\\Desktop\\新建文件夹\\favicon22.ico");

            // 生成图片
            //ImageIO.write(buffImg, "JPG", os);
            return image;

        } catch (Exception e) {
            e.printStackTrace();
        }
        /*finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
        return image;
    }
}
